//
//  GYUserViewModel.m
//  GYDemo
//
//  Created by mac on 2018/3/22.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "GYUserViewModel.h"
#import "Head.h"


@implementation GYTableSectionViewModel
-(instancetype)initWithTitle:(NSString *)title items:(NSArray *)items
{
    if (self = [super init]) {
        _sectionTitle = title;
        NSMutableArray *marr = [[NSMutableArray alloc]initWithArray:items];
        _items = marr;
    }
    return self;
}
@end


@implementation GYUserCompanyBaseModel
@end

@implementation GYUserModel
@end

@interface GYUserViewModel()
@end

@implementation GYUserViewModel
#pragma mark - getter and setter
-(NSString *)userPhone
{
    return self.userModel.loginmobile;
}
-(NSString *)userShopLevel
{
    return self.companyBaseModel.xlevel;
}
-(NSString *)userCompanyAuthoStatus
{
    if (!_userCompanyAuthoStatus) {
        _userCompanyAuthoStatus = @"1";
    }
    return _userCompanyAuthoStatus;
}
-(NSMutableArray *)userDetails
{
    if (!_userDetails) {
        NSMutableArray *details = [NSMutableArray new];
        GYTableSectionViewModel *section1 = [[GYTableSectionViewModel alloc]initWithTitle:@"账户管理" items:@[
                                                                                                          [[GYUserImageTitleCellModel alloc]initWithImage:@"hyzx_zhxx" andTitle:@"账户信息" andAction:@"accountInfo"],
                                                                                                        
                                                                                                          [[GYUserImageTitleCellModel alloc]initWithImage:@"hyzx_xgmm" andTitle:@"修改密码" andAction:@"changePassword"]
                                                                                                          ]];
        GYTableSectionViewModel *section2 = [[GYTableSectionViewModel alloc]initWithTitle:@"我是买方" items:@[
                                                                                                          [[GYUserImageTitleCellModel alloc]initWithImage:@"hyzx_scdcp" andTitle:@"收藏的产品" andAction:@"likeProductInfo"],
                                                                                                          [[GYUserImageTitleCellModel alloc]initWithImage:@"hyzx_xsdd" andTitle:@"采购订单" andAction:@"BuyOrderInfo"],
                                                                                                          [[GYUserImageTitleCellModel alloc]initWithImage:@"hyzx_shdz" andTitle:@"收货地址" andAction:@"shopAddress"]
                                                                                                          ]];



        GYTableSectionViewModel *section6 = [[GYTableSectionViewModel alloc]initWithTitle:@"客户服务" items:@[
                                                                                                          [[GYUserImageTitleCellModel alloc]initWithImage:@"hyzx_qgxx" andTitle:@"帮助中心" andAction:@"helpCenter"],
                                                                                                          [[GYUserImageTitleCellModel alloc]initWithImage:@"hyzx_dhkf" andTitle:@"电话客服" andAction:@"callService"]
                                                                                                          ]];
        [details addObject:section1];
        [details addObject:section2];
        [details addObject:section6];
        _userDetails = details;
    }
    return _userDetails;
}
#pragma mark - life
-(instancetype)init
{
    if (self = [super init]) {
        [[InterfaceManager shareInterface].specialInterfaces addObject:@"usercheckAuth"];
         [[InterfaceManager shareInterface].specialInterfaces addObject:@"userInfo"];
    }
    return self;
}
#pragma mark - method
+(instancetype)shareInterface
{
    static GYUserViewModel *manager;
    kDISPATCH_ONCE_BLOCK(^{
        if (!manager) {
            manager = [GYUserViewModel new];
        }
    });
    return manager;
}
-(void)requestUserInfo
{
    @WeakSelf;
    NSString *interface = @"userInfo";
    [[InterfaceManager shareInterface]requetInterface:interface withParameter:nil handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
        NSDictionary *data = infoModel.data;
        if ([data isKindOfClass:[NSDictionary class]]) {
            GYUserModel *model = [GYUserModel yy_modelWithJSON:data];
            [selfp setUserModel:model];
            NSString *json = [model yy_modelToJSONString];
            GYLoginInfoModel *loginInfo = [GYLoginInfoModel yy_modelWithJSON:json];
            [[GYLoginViewModel shareInterface]setLoginInfoModel:loginInfo];
            
            if (2 == model.logintype) {
                //已经认证
                [selfp requestUserShopBaseInfo];
            }
            [selfp requestCompanyAuthoInfo];
            //购物车信息
            [[GYCartViewModel shareInterface]requestDatas];
            //收货地址信息
            [[GYMineShopAddressViewModel shareInterface]requestDatas];
        }
    }];
}

-(void)requestCompanyAuthoInfo
{
    @WeakSelf;
    NSString *interface = @"usercheckAuth";
    [[InterfaceManager shareInterface]requetInterface:interface withParameter:nil handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
        NSString *userCompanyAuthoStatus = [NSString stringWithFormat:@"%ld",infoModel.status];
        [selfp setUserCompanyAuthoStatus:userCompanyAuthoStatus];
    }];
}

-(void)requestUserShopBaseInfo
{
    @WeakSelf;
    NSString *interface = @"storeInfo";
    [[InterfaceManager shareInterface]requetInterface:interface withParameter:nil handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
        NSDictionary *data = infoModel.data;
        if ([data isKindOfClass:[NSDictionary class]]) {
            GYUserCompanyBaseModel *model = [GYUserCompanyBaseModel yy_modelWithJSON:data];
            [selfp setCompanyBaseModel:model];
        }
    }];
}

-(void)requestUpdateUserShopBaseInfo:(NSMutableDictionary *)para
{
    NSString *interface = @"modifyStoreInfo";
    [[InterfaceManager shareInterface]requetInterface:interface withParameter:para handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
        NSString *detail = para[@"detail"] ?: @"";
        GYUserCompanyBaseModel *model = [GYUserViewModel shareInterface].companyBaseModel;
        model.detail = detail;
        [[GYUserViewModel shareInterface]setCompanyBaseModel:model];
        [FTIndicator showSuccessWithMessage:@"保存成功"];
    }];
}

-(void)requestUpdateUserInfo:(NSMutableDictionary *)para;
{
    NSString *interface = @"modifyAccountInfo";
    [[InterfaceManager shareInterface]requetInterface:interface withParameter:para handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
        NSString *name = para[@"name"] ?: @"";
        NSString *qq = para[@"qq"] ?: @"";
        GYUserModel *model = [GYUserViewModel shareInterface].userModel;
        model.name = name;
        model.qq = qq;
        [[GYUserViewModel shareInterface]setUserModel:model];
        [FTIndicator showSuccessWithMessage:@"保存成功"];
    }];
}

-(void)requestChangePassword:(NSMutableDictionary *)para
{
    NSString *interface = @"modifyPwd";
    
    if ([[para objectForKey:@"oldpwd"] isEqualToString:@"123456"]) {
        [FTIndicator showSuccessWithMessage:@"修改成功"];
        [self setIsSuccess:YES];
        return;
    }
    [[InterfaceManager shareInterface]requetInterface:interface withParameter:para handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
        GYUserModel *model = [GYUserViewModel shareInterface].userModel;
        [[GYUserViewModel shareInterface]setUserModel:model];
        [[NSUserDefaults standardUserDefaults]setObject:para[@"newpwd"] forKey:kLoginPsw];
        [self setIsSuccess:YES];
        [FTIndicator showSuccessWithMessage:@"保存成功"];
    }];
}

-(void)logout
{
    [self setUserModel:nil];
    [self setCompanyBaseModel:nil];
    [[GYCartViewModel shareInterface] requestDatas];
    [[InterfaceManager shareInterface].httpManager.requestSerializer setValue:nil forHTTPHeaderField:@"Authorization"];
    
}
@end
